package net.example.model.contract;

import com.oraycn.omcs.utils.BufferUtils;
import com.oraycn.omcs.utils.SerializeUtils;

import net.example.utils.SerializeHelper;

import java.nio.charset.StandardCharsets;

import io.netty.buffer.ByteBuf;

public class ChangeCommentNameContract {

    public String UnitID ;
    public String CommentName ;

    public ChangeCommentNameContract(){}

    public ChangeCommentNameContract(String unitID,String commentName)
    {
        this.UnitID=unitID;
        this.CommentName=commentName;
    }

    /**
     * 消息转byte
     *
     * @return
     */
    public byte[] serialize() throws Exception {
        byte[] bCommentName = CommentName.getBytes(StandardCharsets.UTF_8);
        byte[] bUnitID = UnitID.getBytes(StandardCharsets.UTF_8);
        int contractLen = 4 + 4 + bCommentName.length + 4 + bUnitID.length;
        ByteBuf body = BufferUtils.newBuffer();
        body.writeInt(contractLen);
        body.writeInt(bCommentName.length);
        body.writeBytes(bCommentName);
        body.writeInt(bUnitID.length);
        body.writeBytes(bUnitID);
        byte[] result = new byte[body.readableBytes()];
        System.arraycopy(body.array(),0,result,0,result.length);
        return result;
    }

    public void deSerialize(byte[] info) throws Exception {
        ByteBuf buffer = SerializeHelper.wrappedBuffer(info);
        buffer.readInt();
        this.CommentName= SerializeUtils.readStrIntLen(buffer);
        this.UnitID=SerializeUtils.readStrIntLen(buffer);
    }
}
